<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ishowshao
 * Date: 12-12-19
 * Time: 上午1:00
 * To change this template use File | Settings | File Templates.
 */
require 'config.php';
session_start();
if (empty($_SESSION['username'])) {
    exit;
}

if (!file_exists(DATA_FILE)) {
    file_put_contents(DATA_FILE, '[]');
}
$json = file_get_contents(DATA_FILE);
$array = json_decode($json, true);
if ($array) {
    $array = array_reverse($array);
    $page = 1;
    if (isset($_GET['page'])) {
        $page = intval($_GET['page']);
    }
    $outputArray = array();
    $start = ($page - 1) * $GLOBALS['page-size'];
    $to = $start + $GLOBALS['page-size'];
    $total = count($array);
    for ($i = $start; $i < $to && $i < $total; $i++) {
        array_push($outputArray, $array[$i]);
    }
    echo json_encode($outputArray);
} else {
    echo json_encode(array());
}
